package com.ymy.thinking.in.spring.dependency.injection;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * Api 构造器的依赖注入
 *
 * @author Ringo
 * @date 2021/9/29 18:24
 */
public class ApiDependencyConstructorInjectionDemo {
    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        beanFactory.registerBeanDefinition("user", createUserBeanDefinition());
        System.out.println(beanFactory.getBean(User.class));

        beanFactory.registerBeanDefinition("userHolder", createUserHolderBeanDefinition());
        System.out.println(beanFactory.getBean(UserHolder.class));
    }

    public static BeanDefinition createUserBeanDefinition() {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(User.class)
                .addConstructorArgValue("张无忌")
                .addConstructorArgValue(18);
        return builder.getBeanDefinition();
    }

    public static BeanDefinition createUserHolderBeanDefinition() {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(UserHolder.class)
                .addConstructorArgReference("user");
        return builder.getBeanDefinition();
    }

}
